using PKGames.Audio;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class MainMenuToggleController : MonoBehaviour
{
	private enum MenuState
	{
		MainMenu,
		MainMenuToSettings,
		Settings,
		SettingsToMainMenu
	}

    public static MainMenuToggleController Instance { get; set; }

	[SerializeField]
	private Button _settingsButton;

	[SerializeField]
	private Image _settingsButtonImage;

	[SerializeField]
	private Sprite _settingsButtonSprite;

	[SerializeField]
	private Sprite _backButtonSprite;

	[SerializeField]
	private CanvasGroup _mainMenuCanvasGroup;

	[SerializeField]
	private CanvasGroup _settingsCanvasGroup;

	private MenuState _menuState;

	private const float FadeTransitionTime = 0.15f;

    public CheckTouch checkTouch;

    private void Start()
	{
		_settingsButton.onClick.AddListener(ToggleSettings);
        Instance = this;
	}

	private void ToggleSettings()
	{
		if (CanChangeState())
		{
			GoToNextState();
		}
	}

	private bool CanChangeState()
	{
		return _menuState != MenuState.MainMenuToSettings && _menuState != MenuState.SettingsToMainMenu;
	}

	private void GoToNextState()
	{
		_menuState = FindNextState(_menuState);
		UpdateAccordingToState(_menuState);
	}

	private MenuState FindNextState(MenuState state)
	{
		switch (state)
		{
		case MenuState.MainMenu:
			return MenuState.MainMenuToSettings;
		case MenuState.MainMenuToSettings:
			return MenuState.Settings;
		case MenuState.Settings:
			return MenuState.SettingsToMainMenu;
		case MenuState.SettingsToMainMenu:
			return MenuState.MainMenu;
		default:
			throw new ArgumentOutOfRangeException();
		}
	}

	private void UpdateAccordingToState(MenuState state)
	{
		switch (state)
		{
		case MenuState.MainMenu:
			SetMainMenuState();
			break;
		case MenuState.MainMenuToSettings:
			SetMainMenuToSettingsState();
			break;
		case MenuState.Settings:
			SetSettingsMenuState();
			break;
		case MenuState.SettingsToMainMenu:
			SetSettingsToMainMenuState();
			break;
		default:
			throw new ArgumentOutOfRangeException("state", state, null);
		}
	}

	private void SetMainMenuState()
	{
		_settingsButtonImage.sprite = _settingsButtonSprite;
		_settingsCanvasGroup.alpha = 0f;
		_settingsCanvasGroup.interactable = false;
		_settingsCanvasGroup.blocksRaycasts = false;
		_mainMenuCanvasGroup.alpha = 1f;
		_mainMenuCanvasGroup.interactable = true;
		_mainMenuCanvasGroup.blocksRaycasts = true;
	}

	private void SetSettingsMenuState()
	{
		_settingsButtonImage.sprite = _backButtonSprite;
		_settingsCanvasGroup.alpha = 1f;
		_settingsCanvasGroup.interactable = true;
		_settingsCanvasGroup.blocksRaycasts = true;
		_mainMenuCanvasGroup.alpha = 0f;
		_mainMenuCanvasGroup.interactable = false;
		_mainMenuCanvasGroup.blocksRaycasts = false;
	}

	private void SetMainMenuToSettingsState()
	{
		MonoSingleton<AudioManager>.Instance.Play("select_simple");
		_settingsCanvasGroup.interactable = false;
		_mainMenuCanvasGroup.interactable = false;
		_settingsCanvasGroup.blocksRaycasts = false;
		_mainMenuCanvasGroup.blocksRaycasts = false;
		StartCoroutine(SetMainMenuToSettingsStateAnimated());
	}

	private IEnumerator SetMainMenuToSettingsStateAnimated()
	{
		yield return CrossFadeCanvasGroups(_mainMenuCanvasGroup, _settingsCanvasGroup, 0.15f);
		GoToNextState();
	}

	private IEnumerator CrossFadeCanvasGroups(CanvasGroup currentCanvas, CanvasGroup nextCanvas, float totalTime)
	{
		AnimationCurve fadeCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
		for (float time = 0f; time < totalTime; time += Time.deltaTime)
		{
			currentCanvas.alpha = fadeCurve.Evaluate(1f - time / totalTime);
			nextCanvas.alpha = fadeCurve.Evaluate(time / totalTime);
			yield return null;
		}
		currentCanvas.alpha = 0f;
		nextCanvas.alpha = 1f;
	}

	private void SetSettingsToMainMenuState()
	{
		MonoSingleton<AudioManager>.Instance.Play("back");
		_settingsCanvasGroup.interactable = false;
		_mainMenuCanvasGroup.interactable = false;
		_settingsCanvasGroup.blocksRaycasts = false;
		_mainMenuCanvasGroup.blocksRaycasts = false;
		StartCoroutine(SetSettingsToMainMenuStateAnimated());
	}

	private IEnumerator SetSettingsToMainMenuStateAnimated()
	{
		yield return CrossFadeCanvasGroups(_settingsCanvasGroup, _mainMenuCanvasGroup, 0.15f);
		GoToNextState();
	}
}
